<template>
	<view> 
		<u-form-item :labelWidth='100' :label="labelName" required borderBottom ref="item1">
			<!-- <u-input :placeholder="placeholder" v-model="gpsaddress" readonly>
				<template slot="suffix">
					<u-icon name="map-fill" color="#2979ff" size="28"></u-icon>
				</template>
			</u-input> -->
			<u--textarea v-model="gpsaddress" placeholder="请输入地址" height='60' disabled></u--textarea>
		</u-form-item>
	</view>
</template>

<script>
	export default{
		props:['labelName','placeholder'],
		data(){
			return{
				gpsaddress:'',
				gpsLat:'',
				gpsLon:''
			}
		},
		methods:{
			//获取定位
				getadress() {
					uni.showLoading({
						title: '地址获取中'
					});
					
					
					
					let that = this;
					uni.getSetting({
						success(res) {
							if (res.authSetting['scope.userLocation'] == false) { //如果没有授权地理位置
								uni.openSetting({
									success(res) {
										res.authSetting = { //打开授权位置页面，让用户自己开启
											"scope.userLocation": true
										}
									}
								})
							} else {
								//用户开启授权后可直接获取地理位置
								uni.authorize({
									scope: 'scope.userLocation',
									success() {
										//获取位置后相关操作
										that.getLocation();
									}
								})
							}
						}
					})
				},
				getLocation() {
					let that = this;
					uni.getLocation({
						type: 'gcj02',
						success: function(res) {
							// 返回的res:name(地理名称）、address（详细地址，包括省市区相关信息，可根据需要进行拆分）、latitude（纬度）、longitude（经度）
							// console.log("地理名称" + JSON.stringify(res));
							let latitude, longitude;
							latitude = res.latitude.toString();
							longitude = res.longitude.toString();
							that.gpsLat = latitude;
							that.gpsLon = longitude;
							uni.request({
								header: {
									"Content-Type": "application/text"
								},
								url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' +
									longitude + '&key=KTXBZ-KLFKX-DG24G-Z42FP-RO2GK-7HBLN',
								success(re) {
									// console.log("地理名称111" + JSON.stringify(res));
									if (re.statusCode === 200) {
										// console.log("地理" + JSON.stringify(re));
										that.gpsaddress = re.data.result.formatted_addresses.recommend +' '+ re.data.result.formatted_addresses
											.standard_address;
									} else {
										uni.showToast({
											title: '获取信息失败，请重试！',
											icon: 'none'
										});
									}
									setTimeout(function () {
										uni.hideLoading();
									}, 1000);
								}
							});
						},
						fail: (res) => {
							let text = "";
							switch (res.errMsg) {
								case "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF": {
									text = "请手动打开手机定位和WIFI"
									break;
								}
								case "getLocation:fail:system permission denied": {
									text = "请允许微信访问定位权限"
									break;
								}
								default: {
									text = "获取位置信息报错:" + res.errMsg
								}
							}
							uni.showToast({
								icon: 'none',
								title: text,
							});
							console.log("fail " + text);
						},
					})
				},
			
		}
	}
	
</script>

<style>
</style>